<?php
namespace api\modules\v1\models\userFund;

use api\modules\common\models\User;
use common\models\Functions;
use common\models\Status;
use yii\base\Exception;
use yii\data\ActiveDataProvider;
use yii\data\Pagination;
use Yii;

/**
 * This is the model class for table "goods".
 *
 * @property int $id
 * @property string $name
 */
class UserMoneyLog extends \common\models\userFund\UserMoneyLog
{

    /**
     * @return array
     */
    public function getMoneyTypeMap()
    {
        try {
            $propId = (int)Yii::$app->request->get('prop_id','');

            $map = Status::USER_CHANGE_TAG_MAP[$propId];
            $map[0] = '全部';

            return Functions::formatRestAPI(0, '', $map);
        } catch (\yii\db\Exception $ee) {
            return Functions::formatRestAPI(1, '数据异常');
        } catch(Exception $e) {
            return Functions::formatRestAPI(1,$e->getMessage());
        }
    }

    public function getList() {
        try {

            $page = (int) Yii::$app->request->post('page',1);
            $size = (int) Yii::$app->request->post('size',20);
            $type = (string) Yii::$app->request->post('type',0);
            $propId = (string) Yii::$app->request->post('prop_id',0);

            $where = $andWhere = [];
            $where['user_id'] = Yii::$app->getUser()->getId();
            if ($type) {
                $where['change_type'] = $type;
            }
            if ($propId) {
                $where['prop_id'] = $propId;
            }
            $data = static::setPaginationForApi($page);

            $offset = ($page - 1) * $size;
            $list = static::getObjectByAndWhere($where,$andWhere, ['*'], 'id DESC', $offset, $size)->with([
            ])->asArray()->all();
            $result = [];
            foreach ($list as $v) {
                $isShowDetail = false;
                if (in_array($v['change_type'],[
                    Status::MONEY_LOG_TYPE_SEND_RED_PACKET,
                    Status::MONEY_LOG_TYPE_RECEIVE_RED_PACKET,
                    Status::MONEY_LOG_TYPE_BACK_RED_PACKET,
                ])) {
                    $isShowDetail = true;
                }
                $result[] = [
                    'id' => (int) $v['id'],
                    'type' => (int) $v['change_type'],
//                    'type_name' => Status::MONEY_LOG_TYPE_MAP[$v['change_type']] ?? '',
                    'type_name' => $v['change_tag'] ?? '',
                    'num' => $v['num'],
                    'order_id' => $isShowDetail ? $v['topic_id'] : 0,
                    'mark' => $v['mark'],
                    'time' => $v['change_time'] ?? '',
                    'after_money' => $v['after_change'] ?? '',
                ];
            }

            $data['data'] = $result;

            return Functions::formatRestAPI(0, '', $data);
        } catch (\yii\db\Exception $ee) {
            return Functions::formatRestAPI(1, '数据异常');
        } catch (Exception $e) {
            return Functions::formatRestAPI(1, $e->getMessage());
        }
    }
}
